<?php

class Ext_Attachment_Music_Add
{
    protected $_articleId;

    protected $_messager;
    const MUSIC_EXISTS              = 'musicExists';
    protected $_messageTemplates = array(
        self::MUSIC_EXISTS              => "Music '%name%' already exists.",
    );
    
    public function __construct($articleId)
    {
        $this->_articleId = $articleId;
        $this->_messager  = new Ext_Messager($this->_messageTemplates);
    }

    public function add($data)
    {
        $result = array(
            'code'     => Ext_Attachment_Music_Add_Result::FAILURE,
            'data'     => '',
            'messages' => array(),
        );
        
        $musicIdArray = array();
        $articleMusic = App_Model_DbTable_Article_Attachment_Music::getInstance()->getRowByArticleId($this->_articleId);
        if($articleMusic){
            $ids = $articleMusic['ids'];

            $idsArray = explode(';', $ids);
            for($i=0;$i<count($idsArray);$i++)
            {
                $attachmentMusicId = $idsArray[$i];
                if(!$attachmentMusicId){
                    continue;
                }

                $attachmentMusic = App_Model_DbTable_Attachment_Music::getInstance()->getRowById($attachmentMusicId);
                if(!$attachmentMusic){
                    continue;
                }
                $musicIdArray[] = $attachmentMusic['musicId'];
            }
        }
        
        $musicId = $data->musicId;
        if(in_array($musicId, $musicIdArray)){
            $title = $data->name;
            if($data->singer){
                $title .= " [".$data->name."]";
            }
            $result['messages'][] = $this->_messager->addMessage(self::MUSIC_EXISTS, array('name'=>$title));
            return new Ext_Attachment_Music_Add_Result($result['code'], $result['data'], $result['messages']);
        }
        
        $row = array(
            'articleId' => $this->_articleId,
            'musicId'   => $data->musicId,
            'name'      => $data->name,
            'singer'    => $data->singer,
        );
        $attachmentMusicId = App_Model_DbTable_Attachment_Music::getInstance()->addNew($row);
        
        $musicr = new Ext_Article_Attachment_Music($this->_articleId);
        $musicr->add($attachmentMusicId);
        
        $article = App_Model_DbTable_Article::getInstance()->getRowById($this->_articleId);
        if(!$article){
            throw new Ext_Publish_Attachment_Add_Exception("Article record not found.");
        }
        
        $attachmentsArray = unserialize($article['attachments']);
        if(!is_array($attachmentsArray)){
            $attachmentsArray = array();
        }
        $attachment = array("type"=>'music', "id"=>$attachmentMusicId);
        array_unshift($attachmentsArray, $attachment);
        if($attachmentsArray){
            $row = array(
                'attachments'  => serialize($attachmentsArray),
            );
            App_Model_DbTable_Article::getInstance()->addOld($row, $this->_articleId);
        }
        
        $result['code'] = Ext_Attachment_Music_Add_Result::SUCCESS;
        $result['data'] = $attachmentMusicId;
        return new Ext_Attachment_Music_Add_Result($result['code'], $result['data'], $result['messages']);
    }
}
